package dao;

import common.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 23050
 * @version 1.0
 * @description:
 * 新增题目
 * 删除题目
 * 查询题目列表
 * 查询题目详情
 * @date 2023/12/22 8:22
 */
public class ProblemDAO {

    //插入题目
    public void insert(Problem problem) {
        Connection connection = null;
        PreparedStatement statement = null;
        try {
            connection = DBUtil.getConnection();
            String sql = "insert into oj_table values (null,?,?,?,?,?)";
            statement = connection.prepareStatement(sql);
            statement.setString(1,problem.getTitle());
            statement.setString(2,problem.getLevel());
            statement.setString(3,problem.getDescription());
            statement.setString(4,problem.getTemplateCode());
            statement.setString(5,problem.getTestCode());
            //3.执行sql
            int ret = statement.executeUpdate();
            if (ret!=1){
                System.out.println("题目新增失败！");
            }else{
                System.out.println("题目新增成功！");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally{
            DBUtil.close(connection,statement,null);
        }


    }

    //删除题目
    public void delete(int id){
        Connection connection = null;
        PreparedStatement statement = null;
        try {
            connection = DBUtil.getConnection();
            String sql = "delete from oj_table where id = ?";
            statement = connection.prepareStatement(sql);
            statement.setInt(1,id);
            //3.执行sql
            int ret = statement.executeUpdate();
            if (ret!=1){
                System.out.println("删除题目失败!");
            }else{
                System.out.println("删除题目成功!");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DBUtil.close(connection,statement,null);
        }
    }

    //把列表中的所有题查出来
    //数据库中的题特别多实现分页查询
    public List<Problem> selectAll(){
        List<Problem> problems = new ArrayList<>();
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        try {
            connection = DBUtil.getConnection();
            String sql = "select `id`,`title`,`level` from oj_table";
            statement = connection.prepareStatement(sql);
            resultSet = statement.executeQuery();
            while (resultSet.next()){
                Problem p =  new Problem();
                p.setId(resultSet.getInt("id"));
                p.setTitle(resultSet.getString("title"));
                p.setLevel(resultSet.getString("level"));
                problems.add(p);
            }
            return problems;
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DBUtil.close(connection,statement,resultSet);
        }

        return null;
    }


    public Problem selectOne(int id){
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;


        try {
            connection = DBUtil.getConnection();
            String sql = "select * from oj_table where id = ?";
            statement = connection.prepareStatement(sql);
            statement.setInt(1,id);
            resultSet = statement.executeQuery();
            if (resultSet.next()){
                Problem p = new Problem();
                p.setId(resultSet.getInt("id"));
                p.setTitle(resultSet.getString("title"));
                p.setLevel(resultSet.getString("level"));
                p.setDescription(resultSet.getString("description"));
                p.setTemplateCode(resultSet.getString("templateCode"));
                p.setTestCode(resultSet.getString("testCode"));
                return p;
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DBUtil.close(connection,statement,resultSet);
        }
        return null;

    }


    public static void testInsert() {
        ProblemDAO problemDao =  new ProblemDAO();
        Problem problem = new Problem();
        problem.setTitle("两数之和");
        problem.setLevel("简单");
        problem.setDescription("给定一个整数数组 nums 和一个整数目标值 target，请你在该数组中找出 和为目标值 target  的那 两个 整数，并返回它们的数组下标。\n" +
                "\n" +
                "你可以假设每种输入只会对应一个答案。但是，数组中同一个元素在答案里不能重复出现。\n" +
                "\n" +
                "你可以按任意顺序返回答案。\n" +
                "\n" +
                " \n" +
                "\n" +
                "示例 1：\n" +
                "\n" +
                "输入：nums = [2,7,11,15], target = 9\n" +
                "输出：[0,1]\n" +
                "解释：因为 nums[0] + nums[1] == 9 ，返回 [0, 1] 。\n" +
                "示例 2：\n" +
                "\n" +
                "输入：nums = [3,2,4], target = 6\n" +
                "输出：[1,2]\n" +
                "示例 3：\n" +
                "\n" +
                "输入：nums = [3,3], target = 6\n" +
                "输出：[0,1]\n" +
                " \n" +
                "\n" +
                "提示：\n" +
                "\n" +
                "2 <= nums.length <= 104\n" +
                "-109 <= nums[i] <= 109\n" +
                "-109 <= target <= 109\n" +
                "只会存在一个有效答案\n" +
                " \n" +
                "\n" +
                "进阶：你可以想出一个时间复杂度小于 O(n2) 的算法吗？");
        problem.setTemplateCode("class Solution {\n" +
                "    public int[] twoSum(int[] nums, int target) {\n" +
                "\n" +
                "    }\n" +
                "}");
        problem.setTestCode("public static void main(String[] args) {\n" +
                "\n" +
                "        //test1\n" +
                "        Solution solution = new Solution();\n" +
                "        int[] nums = {2,7,11,15};\n" +
                "        int target = 9;\n" +
                "        int[] result = solution.twoSum(nums,target);\n" +
                "        if (result.length == 2 && result[0] == 0 && result[1] == 1){\n" +
                "            System.out.println(\"testcase1 OK\");\n" +
                "        }else{\n" +
                "            System.out.println(\"testcase1 failed\");\n" +
                "        }\n" +
                "\n" +
                "        //test2\n" +
                "        int[] nums2 = {3,2,4};\n" +
                "        int target2 = 6;\n" +
                "        int[] result2 = solution.twoSum(nums2,target2);\n" +
                "        if (result2.length == 2 && result2[0] == 1 && result2[1] == 2){\n" +
                "            System.out.println(\"testcase2 OK\");\n" +
                "        }else{\n" +
                "            System.out.println(\"testcase2 failed\");\n" +
                "        }\n" +
                "\n" +
                "\n" +
                "\n" +
                "    }");

        problemDao.insert(problem);
    }

    private static void testSelectAll() {
        ProblemDAO problemDAO = new ProblemDAO();
        List<Problem> problems = problemDAO.selectAll();
        System.out.println(problems);
    }


    private static void testSelectOne() {
        ProblemDAO problemDAO = new ProblemDAO();
        Problem problem = problemDAO.selectOne(1);
        System.out.println(problem);
    }

    private static void testDelete() {
        ProblemDAO problemDao = new ProblemDAO();
        problemDao.delete(1);
    }

    public static void main(String[] args) {
       testInsert();
//        testSelectAll();
//        testSelectOne();
//        testDelete();
    }




}


















